/*
 * Copyright 2009-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License i distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.paoding.rose.jade.statement;

import net.paoding.rose.jade.annotation.SQL;
import net.paoding.rose.jade.annotation.SQLParam;
import net.paoding.rose.jade.annotation.SQLType;
import net.paoding.rose.jade.annotation.condition.SQLCondition;
import net.paoding.rose.jade.jpa.JpaUtil;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * {@link StatementMetaData} 封装、缓存了一个DAO方法的相关信息
 * <p>
 * @author 王志亮 [qieqie.wang@gmail.com]
 */
@SuppressWarnings({"rawtypes"})
public class StatementMetaData {

    /**
     * 所属的DAO类的classMetaData
     */
    private final DAOMetaData daoMetaData;

    /**
     * 所在的DAO方法
     */
    private final Method method;

    /**
     * 方法的参数名顺序
     */
    private final String[] paramNames;

    /**
     * DAO方法上的原始SQL语句，如果没有执行SQL语句，则根据方法签名生成相应的串辅助debug
     */
    private final String sql;

    /**
     * SQL类型（查询类型或者更新类型）：默认由方法名和SQL语句判断，除非强制指定。
     * @see SQLType
     */
    private final SQLType sqlType;

    /**
     * DAO方法的“最低返回类型”。
     * <p>
     * 大部分情况returnType和method.getReturnType是相同的，但对于一些声明为泛型的返回类型，
     * Jade会尽量提取出实际的类型作为returnType
     * <p>
     * 比如：
     *
     * <pre>
     * //@DAO、@SQL注解从略
     * public interface BaseDAO[E] {
     *
     *     public E getById(Long id);
     *
     * }
     * public interface UserDAO extends BaseDAO[User] {
     *
     * }
     * </pre>
     * <p>
     * 此时，UserDAO#getById方法的returnType是User，而非Object;
     */
    private final Class returnType;

    /**
     * 方法返回参数的范型类型（不支持多级）－从method中获取并缓存
     */
    private final Class[] genericReturnTypes;

    /**
     * {@link SQLParam} 注解数组－从method中获取并缓存
     * <p>
     * 此数组的长度为方法的参数个数，如果对应位置的方法参数没有注解 {@link SQLParam},该位置的元素值为null
     */
    private final SQLParam[] sqlParams;

    private final SQLCondition[] sqlConditions;

    private final int parameterCount;

    /**
     * 是否使用 jpa方式解析methodName
     */
    private boolean analysisWithJap = false;

    /**
     * 使用jpa 解析 methodName 详情
     */
    private JpaUtil jpaUtil;

    // --------------------------------------------

    public StatementMetaData(DAOMetaData daoMetaData, Method method) {
        this.daoMetaData = daoMetaData;
        this.method = method;
        this.paramNames = GenericUtils.getMethodParamNames(method);
        this.returnType = GenericUtils.getReturnType(this);
        this.genericReturnTypes = GenericUtils.getActualClass(method.getGenericReturnType(), daoMetaData);
        SQL sqlAnnotation = method.getAnnotation(SQL.class);
        if (Objects.nonNull(sqlAnnotation)) {
            this.sql = sqlAnnotation.value();
            this.sqlConditions = sqlAnnotation.condition();
            this.sqlType = resolveSQLType(sqlAnnotation);
        } else {
            this.analysisWithJap = true;
            this.sqlConditions = new SQLCondition[0];
            this.sql = null;
            this.jpaUtil = new JpaUtil(daoMetaData, method, returnType, genericReturnTypes);
            this.sqlType = this.jpaUtil.sqlType();
        }
        Annotation[][] annotations = method.getParameterAnnotations();
        this.parameterCount = annotations.length;
        this.sqlParams = new SQLParam[annotations.length];
        for (int index = 0; index < annotations.length; index++) {
            for (Annotation annotation : annotations[index]) {
                if (annotation instanceof SQLParam) {
                    this.sqlParams[index] = (SQLParam) annotation;
                }
            }
        }
    }

    public DAOMetaData getDAOMetaData() {
        return daoMetaData;
    }

    public Method getMethod() {
        return method;
    }

    public Class<?> getReturnType() {
        return returnType;
    }

    public String getSQL() {
        return sql;
    }

    public SQLCondition[] getSqlConditions() {
        return this.sqlConditions;
    }

    public int getParameterCount() {
        return parameterCount;
    }

    public SQLParam getSQLParamAt(int argIndex) {
        return sqlParams[argIndex];
    }

    public String getMethodParamNameAt(int argIndex) {
        return paramNames[argIndex];
    }

    public Class<?>[] getGenericReturnTypes() {
        return genericReturnTypes;
    }

    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
        return method.getAnnotation(annotationClass);
    }

    public SQLType getSQLType() {
        return sqlType;
    }

    private SQLType resolveSQLType(SQL sql) {
        SQLType sqlType = sql.type();
        if (sqlType == SQLType.AUTO_DETECT) {
            for (Pattern selectPattern : SELECT_PATTERNS) {
                // 用正则表达式匹配 SELECT 语句
                if (selectPattern.matcher(getSQL()).find() || selectPattern.matcher(getMethod().getName()).find()) {
                    sqlType = SQLType.READ;
                    break;
                }
            }
            if (sqlType == SQLType.AUTO_DETECT) {
                sqlType = SQLType.WRITE;
            }
        }
        return sqlType;
    }

    @SuppressWarnings("SameParameterValue")
    private SQLType resolveSQLType(String sql, SQLType sqlType) {
        if (sqlType == SQLType.AUTO_DETECT) {
            for (Pattern selectPattern : SELECT_PATTERNS) {
                // 用正则表达式匹配 SELECT 语句
                if (selectPattern.matcher(sql).find() || selectPattern.matcher(getMethod().getName()).find()) {
                    sqlType = SQLType.READ;
                    break;
                }
            }
            if (sqlType == SQLType.AUTO_DETECT) {
                sqlType = SQLType.WRITE;
            }
        }
        return sqlType;
    }

    public boolean isAnalysisWithJap() {
        return analysisWithJap;
    }

    public JpaUtil getJpaUtil() {
        return jpaUtil;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof StatementMetaData) {
            StatementMetaData modifier = (StatementMetaData) obj;
            return daoMetaData.equals(modifier.daoMetaData) && method.equals(modifier.method);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return daoMetaData.hashCode() ^ method.hashCode();
    }

    @Override
    public String toString() {
        return daoMetaData.getDAOClass().getName() + '#' + method.getName();
    }

    private static final Pattern[] SELECT_PATTERNS = new Pattern[]{
            Pattern.compile("^\\s*SELECT.*", Pattern.CASE_INSENSITIVE),
            Pattern.compile("^\\s*GET.*", Pattern.CASE_INSENSITIVE),
            Pattern.compile("^\\s*FIND.*", Pattern.CASE_INSENSITIVE),
            Pattern.compile("^\\s*READ.*", Pattern.CASE_INSENSITIVE),
            Pattern.compile("^\\s*QUERY.*", Pattern.CASE_INSENSITIVE),
            Pattern.compile("^\\s*SHOW.*", Pattern.CASE_INSENSITIVE),
            Pattern.compile("^\\s*DESC.*", Pattern.CASE_INSENSITIVE),
            Pattern.compile("^\\s*DESCRIBE.*", Pattern.CASE_INSENSITIVE),
            };
}
